'use strict';

const Controller = require('egg').Controller;

/**
 * 俱乐部申请管理控制器
 * 处理俱乐部申请的增删改查等功能
 */
class A8Controller extends Controller {
  /**
   * 获取俱乐部申请列表
   */
  async list() {
    const { ctx } = this;
    const applications = await ctx.service.a8.findAll();
    ctx.body = applications;
  }

  /**
   * 创建新的俱乐部申请
   * @param {Object} ctx.request.body
   * @param {string} ctx.request.body.name - 姓名
   * @param {string} ctx.request.body.phone - 手机号码
   * @param {string} ctx.request.body.id_card - 身份证号码
   * @param {string} ctx.request.body.driver_license_type - 驾驶证类型
   * @param {string} ctx.request.body.blood_type - 血型
   * @param {string} ctx.request.body.license_obtained_date - 取得当前准驾时间
   * @param {string} ctx.request.body.occupation_type - 职业类型
   * @param {string} [ctx.request.body.company] - 工作单位
   * @param {string} [ctx.request.body.vehicle_info] - 车辆信息
   * @param {string} [ctx.request.body.license_plate] - 车牌号码
   * @param {string} ctx.request.body.vehicle_displacement - 车辆排量
   * @param {string} [ctx.request.body.application_reason] - 申请加入俱乐部理由
   * @throws {422} 信息不完整或格式错误时抛出异常
   */
  async create() {
    const { ctx } = this;
    ctx.validate({
      name: { type: 'string', required: true },
      phone: { type: 'string', required: true },
      id_card: { type: 'string', required: true },
      driver_license_type: { type: 'string', required: true },
      blood_type: { type: 'string', required: true },
      license_obtained_date: { type: 'string', required: true },
      occupation_type: { type: 'string', required: true },
      company: { type: 'string', required: false },
      vehicle_info: { type: 'string', required: false },
      license_plate: { type: 'string', required: false },
      vehicle_displacement: { type: 'string', required: true },
      application_reason: { type: 'string', required: false },
    });

    const result = await ctx.service.a8.create(ctx.request.body);
    if (!result) {
      ctx.throw(422, '申请信息创建失败');
    }
    ctx.body = result;
  }

  /**
   * 更新俱乐部申请信息
   * @param {Object} ctx.request.body
   * @param {number} ctx.request.body.id - 申请记录ID
   * @param {string} [ctx.request.body.name] - 姓名
   * @param {string} [ctx.request.body.phone] - 手机号码
   * @param {string} [ctx.request.body.id_card] - 身份证号码
   * @param {string} [ctx.request.body.driver_license_type] - 驾驶证类型
   * @param {string} [ctx.request.body.blood_type] - 血型
   * @param {string} [ctx.request.body.license_obtained_date] - 取得当前准驾时间
   * @param {string} [ctx.request.body.occupation_type] - 职业类型
   * @param {string} [ctx.request.body.company] - 工作单位
   * @param {string} [ctx.request.body.vehicle_info] - 车辆信息
   * @param {string} [ctx.request.body.license_plate] - 车牌号码
   * @param {string} [ctx.request.body.vehicle_displacement] - 车辆排量
   * @param {string} [ctx.request.body.application_reason] - 申请加入俱乐部理由
   * @throws {404} 申请记录不存在时抛出异常
   * @throws {422} 信息不完整或格式错误时抛出异常
   */
  async update() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'number', required: true },
      name: { type: 'string', required: false },
      phone: { type: 'string', required: false },
      id_card: { type: 'string', required: false },
      driver_license_type: { type: 'string', required: false },
      blood_type: { type: 'string', required: false },
      license_obtained_date: { type: 'string', required: false },
      occupation_type: { type: 'string', required: false },
      company: { type: 'string', required: false },
      vehicle_info: { type: 'string', required: false },
      license_plate: { type: 'string', required: false },
      vehicle_displacement: { type: 'string', required: false },
      application_reason: { type: 'string', required: false },
    });

    const result = await ctx.service.a8.update(
      ctx.request.body.id,
      ctx.request.body
    );

    if (!result) {
      ctx.throw(404, '未找到该申请记录');
    }
    if (result.error) {
      ctx.throw(422, result.error);
    }

    ctx.body = result;
  }

  /**
   * 删除俱乐部申请记录
   * @param {Object} ctx.request.body
   * @param {number} ctx.request.body.id - 申请记录ID
   * @throws {404} 申请记录不存在时抛出异常
   */
  async delete() {
    const { ctx } = this;
    ctx.validate({
      id: { type: 'number', required: true },
    });

    const result = await ctx.service.a8.delete(ctx.request.body.id);
    if (!result) {
      ctx.throw(404, '未找到该申请记录');
    }
    ctx.body = result;
  }
}

module.exports = A8Controller;
    